package com.huika.renrench.control.my.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseFra;
import com.huika.renrench.control.my.activity.UserInviteActivity;
import com.huika.renrench.control.my.adapter.ColleStoreAdapter;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.ColleCtcarveBean;
import com.huika.renrench.support.bean.MyInvestmentbean;

/**
 * @author yangyuan
 * @date：2015年10月22日
 * @describe:我的推荐----推荐奖励
 */
public class ColleStoreFra extends BaseFra implements OnRefreshListener2<ListView>,Listener<RequestResult<ArrayList<ColleCtcarveBean>>>{

	private RelativeLayout null_layout;
	private PullToRefreshListView ptrLv;
	private ColleStoreAdapter adapter;
	private boolean flag = true;//避免二次加载 
	private int page = 1;
	private int pagesize = 15;
	
	private ArrayList<ColleCtcarveBean> list = new ArrayList<ColleCtcarveBean>();
	
	@Override
	protected View inflaterView(LayoutInflater inflater, ViewGroup container,
			Bundle bundle) {
		return inflater.inflate(R.layout.colle_ctcarve_fra, container, false);
	}
	
	@Override
	protected void initWidget(View parentView) {
		super.initWidget(parentView);
		ptrLv = (PullToRefreshListView) parentView.findViewById(R.id.ptrlv_ilearnthe);
		null_layout = (RelativeLayout) parentView.findViewById(R.id.null_layout);
		
		ptrLv.setMode(Mode.DISABLED);
		adapter = new ColleStoreAdapter((UserInviteActivity)getActivity());
		ptrLv.setAdapter(adapter);
		ptrLv.setOnRefreshListener(this);
		showLoadingDialog("加载中");
		net();
	}

	private void net1(){
		RequestResult<ArrayList<ColleCtcarveBean>> rrRequestResult = new RequestResult<ArrayList<ColleCtcarveBean>>();
		rrRequestResult.flag = 1;
		ArrayList<ColleCtcarveBean> alist = new ArrayList<ColleCtcarveBean>();
		for (int i = 0; i < 10; i++) {
			ColleCtcarveBean bbbbBean = new ColleCtcarveBean();
			bbbbBean.setIdN(132456);
			bbbbBean.setName("小丫头");
			bbbbBean.setTimeStr("2015-10-22 16:03:45");
			bbbbBean.setAward(2000);
			alist.add(bbbbBean);
		}
		rrRequestResult.setRs(alist);
		onResponse(rrRequestResult);
	}
	
	@Override
	protected void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.click_reload:
			net();
			showLoadingDialog("加载中");
			break;
		}
	}

	private void net(){
		if (flag) {
		flag = false;
		FormResultRequest<ArrayList<ColleCtcarveBean>> request = new FormResultRequest<ArrayList<ColleCtcarveBean>>(UrlConstants.MYREWARDS, this, this, new TypeToken<RequestResult<ArrayList<ColleCtcarveBean>>>() {
		}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", GlobalApp.getInstance().getUserId());
		ajaxParams.putCommonTypeParam("pageNo", page);
		ajaxParams.putCommonTypeParam("pageSize", pagesize);//每页数
		request.setRequestParams(ajaxParams);
		executeRequest(request);
		}
	}
	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> arg0) {
		//上拉
		page = 1;
		net();
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> arg0) {
		//下拉
		page++;
		net();
	}

	@Override
	public void onResponse(RequestResult<ArrayList<ColleCtcarveBean>> arg0) {
		dismissLoadingDialog();
		flag = true;
		null_layout.setVisibility(View.GONE);
		ptrLv.onRefreshComplete();
		ptrLv.setMode(Mode.DISABLED);
		if (arg0.flag==1) {
			ArrayList<ColleCtcarveBean> beanlist = arg0.getRs();
			if (beanlist!=null) {
				if (page==1) {
					list.clear();
				}
			list.addAll(beanlist);
			adapter.clearGroup(true);
			adapter.setGroup(list);
			if (adapter.getCount()==0&&list.size()==0) {
				null_layout.setVisibility(View.VISIBLE);
				TextView my_attention_null_tv =  (TextView) null_layout.findViewById(R.id.my_attention_null_tv);
				my_attention_null_tv.setText("没有推荐奖励记录");
			}else {
//				if (adapter.getCount()>0) {
//					showToastMsg("刷新成功");
//				}
				if (beanlist.size()<15) {
					ptrLv.setMode(Mode.PULL_DOWN_TO_REFRESH);
				}else {
					ptrLv.setMode(Mode.BOTH);
				}
			}
		  }else {
			  if (page>1) {
				  page--;
			}
			  if (adapter.getCount()==0) {
					null_layout.setVisibility(View.VISIBLE);
					TextView my_attention_null_tv =  (TextView) null_layout.findViewById(R.id.my_attention_null_tv);
					my_attention_null_tv.setText("没有推荐奖励记录");
				}
		    }
		}else {
			  if (page>1) {
				  page--;
			}
		}
	}
	
	@Override
	public void onErrorResponse(VolleyError arg0) {
		flag = true;
		null_layout.setVisibility(View.GONE);
		dismissLoadingDialog();
		ptrLv.onRefreshComplete();
		  if (page>1) {
			  page--;
		}
		if (adapter.getCount()>0) {
			showToastMsg("下载错误");
		}else {
			showIndexError();
			ptrLv.setMode(Mode.DISABLED);
		}
	}
	
	private void showIndexError() {
		if (mOverlayLayout == null) {
			setOverLayAttachTo(ptrLv);
			setNetErrorClick(this);
		}else {
			showOverLay();
		}
	}
	
}